package com.easyj.base.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

public class ScaleValidator implements ConstraintValidator<Scale, BigDecimal> {
    private int scale=0; 
    @Override
	public void initialize(Scale scale) {
		this.scale = scale.value();
	}
    
	@Override
	public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
		if(value==null) {
			return true;
		}
		value=value.stripTrailingZeros();//
		String str=value.toPlainString();
		int index = str.indexOf(".");
	    int len= index < 0 ? 0 : str.length() - index - 1;
	    boolean bl=len<=scale;
		return bl;
		
	}
	
}
